package com.yuang.redis;

import redis.clients.jedis.Jedis;

import java.rmi.registry.LocateRegistry;
import java.util.*;

/**
 * Hello world!
 *
 */
public class App 
{

    static Jedis jedis = null;
    static {
        jedis = new Jedis("localhost");
    }
    public void testRedisString() {
        System.out.println("============testRedisString============");
        jedis.set("城市","南京");
        jedis.set("城市","广州");
        System.out.println("testRedisString():"+jedis.get("城市"));
    }

    public void testRedisList() {
        System.out.println("============testRedisList============");
        jedis.lpush("site-list","baidu");
        jedis.lpush("site-list","google");
        jedis.rpush("site-list","apple");
        List<String> list = jedis.lrange("site-list",0,2);
        System.out.println("print the list");
        for(int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    public void testRedisSet() {
        System.out.println("============testRedisSet============");
        jedis.sadd("myset","a");
        jedis.sadd("myset","a");
        jedis.sadd("myset","b");
        //获取所有元素
        Set<String> set = jedis.smembers("myset");
        System.out.println(set);
        //返回集合个数
        System.out.println(jedis.scard("myset"));
        //判断myset中是否有元素b
        System.out.println(jedis.sismember("myset","b"));
        //出栈
        System.out.println(jedis.spop("myset"));

        jedis.sadd("myset2","a");
        jedis.sadd("myset2","c");
        //交集
        System.out.println("交集:"+jedis.sinter("myset","myset2"));
        //并集
        System.out.println("并集:"+jedis.sunion("myset","myset2"));
        //差集
        System.out.println("差集:"+jedis.sdiff("myset","myset2"));

    }

    public void testRedisHash() {
        System.out.println("============testRedisHash============");
        Map<String,String> pairs = new HashMap<String, String>();
        pairs.put("name","Yuang");
        pairs.put("age","30");
        pairs.put("sex","Female");
        jedis.hmset("people",pairs);

        jedis.hset("student","name","mic");
        jedis.hset("student","age","29");
        jedis.hset("student","sex","Male");

        System.out.println("hash obj people's name:"+jedis.hmget("people","name"));
        System.out.println(jedis.hget("people","age"));
        System.out.println("key为people的hash obj个数:"+jedis.hlen("people"));
        System.out.println("exist people? "+jedis.exists("people"));
        System.out.println("all keys of hash obj people:"+jedis.hkeys("people"));
        System.out.println("all value of hash obj people:"+jedis.hvals("people"));

        //print all property
        Iterator<String> it = jedis.hkeys("people").iterator();
        while(it.hasNext()) {
            String key = it.next();
            System.out.println(key+":"+jedis.hmget("people",key));
        }
    }

    public void getAllKeys() {
        System.out.println("============getAllKeys============");
        Set<String> keys = jedis.keys("*");
        Iterator<String> it = keys.iterator();
        while(it.hasNext()) {
            String key = it.next();
            System.out.println("key:"+key);
        }
    }

    public static void main( String[] args ) {
        App app = new App();
        app.testRedisString();
        app.testRedisList();
        app.testRedisSet();
        app.testRedisHash();
        app.getAllKeys();

    }
}
